package com.verycd.util
{
	import com.net.AssetData;
	import com.ui.controls.Alert;
	import com.ui.core.Align;
	import com.ui.data.AlertData;
	import com.ui.data.TextInputData;
	import com.ui.skin.SkinStyle;
	import com.utils.ItemCssType;

	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.clearTimeout;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;

	/**
	 * @author Halley
	 */
	public class AlertUtil {
		public static var yesNoButtons : uint = Alert.YES | Alert.NO;
		public static var okNoneButtons : uint = Alert.OK | Alert.CANCEL;
		public static const VIEW : String = "ui";
		/**
		 * 常用按钮 按下
		 */
		public static const BUTTON_DOWN : String = "button_down";
		/**
		 * 常用按钮 弹起
		 */
		public static const BUTTON_UP : String = "button_up";
		/**
		 * 常用按钮 进入
		 */
		public static const BUTTON_OVER : String = "button_over";
		/**
		 * 外部边框
		 */
		public static const FRAME_OUT_BG : String = "frame_out_bg";
		public static var root : DisplayObjectContainer;

		private static function createData(msg : String, title : String = "", isModel : Boolean = false, flag : uint = 4) : AlertData {
			var data : AlertData = new AlertData();
			data.panelData.padding = 30;
			var inner : Rectangle = new Rectangle(20, 40, 30, 360);
			var assetData : AssetData = new AssetData("frame_out_show");
			assetData.isScaleBitmap = true;
			assetData.inner = inner;
			data.panelData.bgAsset = assetData;
			data.parent = root;
			data.width = 380;
			data.titleBarData.labelData.text = title;
			data.titleBarData.width = 380;
			data.titleBarData.backgroundAsset = new AssetData(SkinStyle.emptySkin);
			
			// data.titleBarData.labelData.align = Align.CENTER;

			data.buttonData.upAsset = new AssetData(BUTTON_UP, VIEW);
			data.buttonData.downAsset = new AssetData(BUTTON_DOWN, VIEW);
			data.buttonData.overAsset = new AssetData(BUTTON_OVER, VIEW);

			data.minWidth = 380;
			data.align = Align.CENTER;
			data.labelData.width = 325;
			data.labelData.htmlText = msg;
			data.labelData.styleSheet = ItemCssType.css();
			// data.labelData.iconData = new IconData();
			// data.labelData.iconData.bitmapData = BDUtil.getBD(new AssetData("light_22"));
			data.flag = flag;
			data.allowDrag = true;
			data.showShadow = isModel;
			data.closeButtonData.visible = false;
			return data;
		}

		private static function alert(data : AlertData, callBack : Function = null, isInput : Boolean = false, delay : uint = 0, delayFun : Function = null) : Alert {
			var alert : Alert = new Alert(data);
			alert.addEventListener(Event.CLOSE, function(event : Event) : void {
				delete uniqueAlertDic[data.labelData.htmlText];
				if (callBack != null) {
					if (isInput)
						callBack(alert.detail, alert.textInput.text);
					else
						callBack(alert.detail);
				}
			});
			if (delay != 0) {
				var id : uint = setTimeout(function() : void {
					if (delayFun != null) delayFun();
					alert.hide();
					clearTimeout(id);
				}, delay);
			}
			alert.show();
			return alert;
		}

		public static function showTextInut(msg : String, title : String = null, isModel : Boolean = false, flag : uint = 4, callBack : Function = null) : void {
			var data : AlertData = createData(msg, title, isModel, flag);
			data.textInputData = new TextInputData();
			data.textInputData.borderAsset = new AssetData("show_show", "ui");
			data.textInputData.width = 150;
			data.textInputData.height = 25;
			data.labelData.text = msg;
			var alertPanel : Alert = alert(data, callBack, true);
			alertPanel.textInput.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			alertPanel.textInput.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			disableHotKey = true;
		}

		private static function set disableHotKey(value : Boolean) : void {
			var DynamicConfig : * = getDefinitionByName("com.verycd.definition.DynamicConfig");
			if (DynamicConfig)
				DynamicConfig.disableHotKey = value;
		}

		private static function onFocusIn(event : FocusEvent) : void {
			disableHotKey = true;
		}

		private static function onFocusOut(event : FocusEvent) : void {
			disableHotKey = false;
		}

		private static var uniqueAlertDic : Dictionary = new Dictionary(true);

		public static function showUnique(msg : String, title : String = "", isModel : Boolean = false, flag : uint = 4, callBack : Function = null, delay : uint = 0, delayFun : Function = null) : Alert {
			if (uniqueAlertDic[msg]) {
				return null;
			}
			var alertUi : Alert = alert(createData(msg, title, isModel, flag), callBack, false, delay, delayFun);
			uniqueAlertDic[msg] = alertUi;
			return alertUi;
		}

		public static function show(msg : String, title : String = "", isModel : Boolean = false, flag : uint = 4, callBack : Function = null, delay : uint = 0, delayFun : Function = null) : Alert {
			return showUnique(msg, title, isModel, flag, callBack, delay, delayFun);
			// return alert( createData( msg, title, isModel, flag ), callBack, false, delay, delayFun );
		}
	}
}
